EnsureSConsVersion( 1, 1, 0 )

import platform
import os
import re
from os.path import join, dirname, abspath

import SCons.Builder
import SCons.Node.FS
import SCons.Util

root_dir = dirname(File('SConstruct').rfile().abspath)
# --- options ----

options = {}

options_topass = {}

def GuessOS():
    id = platform.system()
    if id == 'Linux':
        return 'linux'
    elif id == 'Windows' or id == 'Microsoft':
        return 'win32'
    elif id == 'AIX':
        return 'aix'
    else:
        return None


def GuessArch():
    id = platform.machine()
    id = id.lower()
    if (not id) or (not re.match('(x|i[3-6])86$', id) is None):
        return 'ia32'
    elif id == 'i86pc':
        return 'ia32'
    elif id == 'x86_64':
        return 'ia64'
    elif id == 'amd64':
        return 'ia64'
    elif id == 'ppc64':
        return 'ppc64'
    elif id == 'ppc64le':
      return 'ppc64le'        
    else:
        return None

guess_os = GuessOS()
guess_arch = GuessArch()

def add_option( name, help , nargs , contibutesToVariantDir , dest=None ):

    if dest is None:
        dest = name

    AddOption( "--" + name , 
               dest=dest,
               type="string",
               nargs=nargs,
               action="store",
               help=help )

    options[name] = { "help" : help ,
                      "nargs" : nargs ,
                      "contibutesToVariantDir" : contibutesToVariantDir ,
                      "dest" : dest }

def get_option( name ):
    return GetOption( name )

def _has_option( name ):
    x = get_option( name )
    if x is None:
        return False

    if x == False:
        return False

    if x == "":
        return False

    return True

def has_option( name ):
    x = _has_option(name)

    if name not in options_topass:
        # if someone already set this, don't overwrite
        options_topass[name] = x

    return x

def get_variant_dir():

    a = []

    for name in options:
        o = options[name]
        if not has_option( o["dest"] ):
            continue
        if not o["contibutesToVariantDir"]:
            continue

        if o["nargs"] == 0:
            a.append( name )
        else:
            x = get_option( name )
            x = re.sub( "[,\\\\/]" , "_" , x )
            a.append( name + "_" + x )

    s = "#build/${PYSYSPLATFORM}/"

    if len(a) > 0:
        a.sort()
        s += "/".join( a ) + "/"
    else:
        s += "normal/"
    return s

def findVersion( root , choices ):
    if not isinstance(root, list):
        root = [root]
    for r in root:
        for c in choices:
            if ( os.path.exists( r + c ) ):
                return r + c
    raise RuntimeError("can't find a version of [" + repr(root) + "] choices: " + repr(choices))

# installation options
add_option( "language" , "description language" , 1 , False )
add_option( "64" , "whether to force 64 bit" , 0 , True , "force64" )
add_option( "32" , "whether to force 32 bit" , 0 , True , "force32" )

if not os.path.exists("bin"):
    os.mkdir("bin")

Export("has_option")
Export("get_option")
Export("GuessOS")
Export("GuessArch")
Export("findVersion")
SConscript( 'SConscript', variant_dir='bin', duplicate=False )
